package com.common.frame.base

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter

class CommonFragmentStateAdapter(
    fm: FragmentManager,
    lifecycle: Lifecycle,
    var fragments: MutableList<Fragment>
) : FragmentStateAdapter(fm, lifecycle) {

    constructor(
        activity: FragmentActivity,
        fragments: MutableList<Fragment>
    ) : this(activity.supportFragmentManager, activity.lifecycle, fragments)

    constructor(
        fragment: Fragment,
        fragments: MutableList<Fragment>
    ) : this(fragment.childFragmentManager, fragment.lifecycle, fragments)

    override fun getItemCount(): Int = fragments.size

    override fun createFragment(position: Int): Fragment = fragments[position]

}